<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="首字母" prop="firstLetter">
        <el-input
          v-model="queryParams.firstLetter"
          placeholder="请输入首字母"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pms:brand:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pms:brand:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pms:brand:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pms:brand:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="brandList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="名称" width="210" align="center" prop="name" />
      <el-table-column
        label="首字母"
        align="center"
        width="65"
        prop="firstLetter"
      />
      <el-table-column label="品牌商" align="center" prop="factoryStatus">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.factoryStatus"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="显示" align="center" prop="showStatus">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.showStatus"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="产品数量" align="center" prop="productCount" />
      <el-table-column label="logo" align="center" prop="logo">
        <template width="60" slot-scope="scope">
          <img
            style="width: 50px; height: 50px; border: none"
            :src="scope.row.logo"
            @click="openUploadDialog()"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pms:brand:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pms:brand:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改品牌对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首字母" prop="firstLetter">
              <el-input v-model="form.firstLetter" placeholder="请输入首字母" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="8">
            <el-form-item label="排序" prop="sort">
              <el-input-number
                v-model="form.sort"
                placeholder="请输入排序"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="品牌商"
              prop="factoryStatus"
              style="width: 50px;padding-left:100px;"
            >
              <el-switch
                v-model="form.factoryStatus"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="显示" prop="showStatus" style="width: 80px">
              <el-switch
                v-model="form.showStatus"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品数量" prop="productCount">
              <el-input-number
                v-model="form.productCount"
                placeholder="请输入产品数量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评论数量" prop="productCommentCount">
              <el-input-number
                v-model="form.productCommentCount"
                placeholder="请输入评论数量"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="logo" prop="logo">
              <el-upload
                action="#"
                :http-request="requestUpload"
                :show-file-list="false"
                :before-upload="beforeUpload"
              >
                <div class="uploadpic">
                  <img v-bind:src="form.logo" title="点击上传" class="img-lg" />
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专区大图" prop="bigPic">
              <el-upload
                action="#"
                :http-request="requestUploadBigpic"
                :show-file-list="false"
                :before-upload="beforeUpload"
              >
                <div class="uploadpic">
                  <img
                    v-bind:src="form.bigPic"
                    title="点击上传"
                    class="img-lg"
                  />
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="品牌故事" prop="brandStory">
          <el-input
            v-model="form.brandStory"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBrand,
  getBrand,
  delBrand,
  addBrand,
  updateBrand,
  uploadPic,
} from "@/api/pms/brand";

export default {
  name: "Brand",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 品牌表格数据
      brandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        firstLetter: null,
        sort: null,
        factoryStatus: null,
        showStatus: null,
        productCount: null,
        productCommentCount: null,
        logo: null,
        bigPic: null,
        brandStory: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询品牌列表 */
    getList() {
      this.loading = true;
      listBrand(this.queryParams).then((response) => {
        this.brandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 默认的上传行为
    requestUpload(param) {
      let formData = new FormData();
      formData.append("uploadfile", param.file);
      uploadPic(formData).then((response) => {
        this.form.logo = response.imgUrl;
      });
    },

    requestUploadBigpic(param) {
      let formData = new FormData();
      formData.append("uploadfile", param.file);
      uploadPic(formData).then((response) => {
        this.form.bigPic = response.imgUrl;
      });
    },

    // 上传预处理
    beforeUpload(file) {},

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        firstLetter: null,
        sort: null,
        factoryStatus: 1,
        showStatus: 1,
        productCount: null,
        productCommentCount: null,
        logo: "http://192.168.56.200:9000/ry-file/2022/02/05/b68e66b4-f1fc-4305-85bc-6b8c6fdb8aea.png",
        bigPic:
          "http://192.168.56.200:9000/ry-file/2022/02/05/b68e66b4-f1fc-4305-85bc-6b8c6fdb8aea.png",
        brandStory: null,
      };

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;

      this.title = "添加品牌";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBrand(id).then((response) => {
        this.form = response.data;
        //有一些大图是空的
        if ([null, undefined, ""].includes(this.form.bigPic)) {
          this.form.bigPic =
            "http://192.168.56.200:9000/ry-file/2022/02/05/b68e66b4-f1fc-4305-85bc-6b8c6fdb8aea.png";
        }

        this.open = true;
        this.title = "修改品牌";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBrand(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBrand(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除品牌编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBrand(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pms/brand/export",
        {
          ...this.queryParams,
        },
        `brand_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
.uploadpic {
  position: relative;
  display: inline-block;
  height: 120px;
}

.uploadpic:hover:after {
  content: "点击上传";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #eee;
  background: rgba(0, 0, 0, 0.5);
  font-size: 30px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  line-height: 120px;
}
</style>